package org.lboot.flow.test;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.lboot.core.auth.AuthService;
import org.lboot.core.domain.ResponseDTO;
import org.lboot.flow.FlowCtl;
import org.lboot.flow.constant.FlowAssignConst;
import org.lboot.flow.module.assign.FlowTaskAssign;
import org.lboot.flow.module.assign.FlowTaskAssignService;
import org.lboot.flow.module.audit.FlowAudit;
import org.lboot.flow.module.audit.FlowAuditService;
import org.lboot.flow.module.define.FlowDefine;
import org.lboot.flow.module.define.FlowDefineService;
import org.lboot.flow.module.work.FlowWork;
import org.lboot.flow.module.work.FlowWorkService;
import org.lboot.flow.module.work.params.FlowDecisionTaskParams;
import org.lboot.flow.module.work.params.FlowStartParams;
import org.lboot.flow.module.work.params.FlowTaskParams;
import org.lboot.mrest.annotation.Get;
import org.lboot.mrest.annotation.Post;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("test")
@Api(tags = "步骤测试接口")
@AllArgsConstructor
public class FlowStepTestController {
    FlowCtl flowCtl;

    AuthService authService;

    FlowWorkService flowWorkService;

    FlowAuditService auditService;

    FlowTaskAssignService assignService;

    FlowDefineService defineService;

    @GetMapping("define/{code}")
    @ApiOperation(value = "查询流程定义")
    public ResponseDTO<Object> queryDefine(@PathVariable("code") String code){
        return ResponseDTO.succData(
                defineService.getFlowByCode(code)
        );
    }

    @PostMapping("flow/start")
    @ApiOperation(value = "发起流程")
    public ResponseDTO<Object> flowStart(@Validated @RequestBody FlowStartParams startParams){
        return ResponseDTO.succData(
                flowCtl.startFlow(startParams)
        );
    }

    @PostMapping("flow/pass")
    @ApiOperation(value = "通过流程")
    public ResponseDTO<Object> flowPass(@Validated @RequestBody FlowTaskParams params){
        // String flowWorkId = params.getFlowWorkId();
        flowCtl.passTask(params);
        return ResponseDTO.succMsg("执行完毕");
    }

    @PostMapping("flow/reject")
    @ApiOperation(value = "拒绝流程")
    public ResponseDTO<Object> flowReject(@Validated @RequestBody FlowTaskParams params){
        // String flowWorkId = params.getFlowWorkId();
        flowCtl.rejectTask(params);
        return ResponseDTO.succMsg("执行完毕");
    }

    @PostMapping("flow/decision")
    @ApiOperation(value = "决策流程")
    public ResponseDTO<Object> flowDecision(@Validated @RequestBody FlowDecisionTaskParams params){
        // String flowWorkId = params.getFlowWorkId();
        flowCtl.decisionTask(params);
        return ResponseDTO.succMsg("执行完毕");
    }



    @GetMapping("flow/{id}")
    @ApiOperation(value = "流程实例信息")
    public ResponseDTO<Object> queryFlow(@PathVariable("id") String id){
        return ResponseDTO.succData(
                flowWorkService.getOneUnwrap(id)
        );
    }

    @GetMapping("flow/{id}/audits")
    @ApiOperation(value = "流程审计信息")
    public ResponseDTO<Object> queryFlowAudits(@PathVariable("id") String id){
        return ResponseDTO.succData(
                auditService.getByFlowWorkId(id)
        );
    }


    @PostMapping("flow/{code}/node/{node}/assign")
    @ApiOperation(value = "新增指派本人(用户)")
    public ResponseDTO<Object> createAssign(@PathVariable("code") String code, @PathVariable("node") String node){
        flowCtl.createAssign(code, node, FlowAssignConst.USER, authService.getUid());
        return ResponseDTO.succData("新增成功");
    }

    @GetMapping("flow/{code}/assigns")
    @ApiOperation(value = "查询指派列表")
    public ResponseDTO<Object> queryAssigns(@PathVariable("code") String code){
        return ResponseDTO.succData(
                assignService.getByFlowCode(code)
        );
    }

    @DeleteMapping("flow/assigns/{id}")
    @ApiOperation(value = "删除指派")
    public ResponseDTO<Object> deleteAssign(@PathVariable("id") String id){
        flowCtl.deleteAssign(id);
        return ResponseDTO.succMsg("删除成功");
    }



}
